package com.yyn.server.dao;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ClientSendThread extends Thread {
    private String name;
    private SocketChannel socketChannel;

    public ClientSendThread(String name, SocketChannel socketChannel) {
        this.name = name;
        this.socketChannel = socketChannel;
    }

    /**
     * 处理发送消息的业务
     */
    @Override
    public void run() {

        try {
            // 创建Buffer
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            // 输入待发送消息
            while (true) {
                Scanner scanner = new Scanner(System.in);
                String msg = scanner.next();
                if (msg != null) {
                    msg = name + ": " + msg;
                    socketChannel.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
